package top.smallway.bs_vr.recycelviewAdapeter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import top.smallway.bs_vr.Arranges.Message;
import top.smallway.bs_vr.R;

public class MessageAdapeter extends RecyclerView.Adapter<MessageAdapeter.MyViewHolder> {
    private List<Message> message;
    private Context context;
    public MessageAdapeter(List<Message> message, Context context){
        this.message=message;
        this.context=context;

    }
    public void updateData(List<Message> newData) {
        message.clear(); // 清空原有数据
        message.addAll(newData); // 添加新的数据
        notifyDataSetChanged(); // 通知RecyclerView刷新界面
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.message_items, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Message message1 = message.get(position);
        holder.message_.setText(message1.getMessage());

    }

    @Override
    public int getItemCount() {
        return message == null ? 0 : message.size();
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView message_;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            message_=itemView.findViewById(R.id.message_);
        }
    }

}
